﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp.Extension.Web.Pages.SettingManagement.EmailSetting;
using Volo.Abp.Extension.Web.Pages.SettingManagement.SiteSetting;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.Emailing;
using Volo.Abp.SettingManagement;
using Volo.Abp.SettingManagement.Web.Pages.SettingManagement;

namespace Volo.Abp.Extension.Web.Pages.SettingManagement
{
    public class IndexModel : AbpPageModel
    {
        public SettingPageCreationContext SettingPageCreationContext { get; private set; }

        private readonly SettingManagementPageOptions _options;
        private readonly ISettingManager settingManager;
        private readonly IServiceProvider _serviceProvider;

        public IndexModel(
            ISettingManager settingManager,
            IOptions<SettingManagementPageOptions> options,
            IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _options = options.Value;

            this.settingManager = settingManager;
        }

        public async Task OnGetAsync()
        {
            SettingPageCreationContext = new SettingPageCreationContext(_serviceProvider);

            foreach (var contributor in _options.Contributors)
            {
                await contributor.ConfigureAsync(SettingPageCreationContext);
            }
        }
        [BindProperty]
        public SiteSettingDto SiteSetting { get; set; }
        [BindProperty]
        public EmailSettingDto EmailSetting { get; set; }

        public async Task<IActionResult> OnPostSiteSettingAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            await settingManager.SetGlobalAsync(SiteSettingNames.SiteName, SiteSetting.SiteName);
            await settingManager.SetGlobalAsync(SiteSettingNames.SiteDesc, SiteSetting.SiteDesc);
            return RedirectToPage("./Index", "", "1");
        }

        public async Task<IActionResult> OnPostEmailSettingAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            await this.settingManager.SetGlobalAsync(EmailSettingNames.DefaultFromAddress, EmailSetting.DefaultFromAddress);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.DefaultFromDisplayName, EmailSetting.DefaultFromDisplayName);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.Host, EmailSetting.SmtpHost);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.Port, EmailSetting.SmtpPort);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.UserName, EmailSetting.SmtpUserName);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.Password, EmailSetting.SmtpPassword);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.Domain, EmailSetting.SmtpDomain);
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.EnableSsl, EmailSetting.SmtpEnableSsl.ToString());
            await this.settingManager.SetGlobalAsync(EmailSettingNames.Smtp.UseDefaultCredentials, EmailSetting.SmtpUseDefaultCredentials.ToString());

            return RedirectToPage("./Index", "", "2");
        }

    }
}
